/*
 * HighScore.java
 *
 * Created on 2007. mo?=rcius 12., 16:49
 *
 */

package laborhazi;

import java.io.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.StringTokenizer;


/**
 *
 * @author nyuszik
 */
public class HighScore {
	
	private BufferedReader in = null;
	private String filename = null;
    private Game game = null;
    private Pattern p;
    private Matcher m;
    private boolean b;
    private Table myTable = null;
    //private StringTokenizer st;
	public int actual = 0;
    private final int MAXLINES = 200;
    public String[] scores = null;
    private PrintStream out = null;
    /** Creates a new instance of HighScore */
    //public HighScore(Game g) {}
    public HighScore(Game g) {
    	game = g;
    	filename = "scores.in";
    	myTable = new Table();
    	
    	/* open file if available */
    	try {
			in = new BufferedReader(new FileReader(filename));
	    	
		} catch (FileNotFoundException e) {
			System.err.println("File not found -> scores.in");
		}
		try {
			if(in != null) {
				parse(in);
			}
		} catch (Exception e) {
			try {
				in.close();
			} catch (IOException e1) {
				System.err.println("Check file permissions on scores.in");
			}
			System.exit(-1);
		}

    }
    /** save the highscore to file
     * @param game the Game
     */
    public void save(Game game) {
    	game.inputName();
    }
    /** adds current score to the database
     * @param name the name of the user
     * @param score the score of the user
     */
    public void add() { 
    	int i;
    	File myFile = null;
    	myFile = new File("scores.in");
    	try {
    	    myFile.delete();
    	} catch (SecurityException e) {
    		System.err.println("Check file permissions on scores.in");
			System.exit(-1);
    	}
    	myFile = new File("scores.in");
    	try {
			myFile.createNewFile();
		} catch (IOException e1) {
			System.err.println("scores.in missing or corrupted");
			System.exit(-1);
		}
    	if(game.stage.getFinish() == null) {
    		myTable.add(new Element(0, game.getName()));
    	} else {
    		myTable.add(new Element(game.stage.getFinish().getScore(), game.getName()));;
    	}
    	myTable.sort();
    	try {
			out =
			    new PrintStream(
			      new BufferedOutputStream(
			        new FileOutputStream("scores.in", true)));
		} catch (SecurityException e) {
			System.err.println("Check file permissions on scores.in");
			System.exit(-1);
		} catch (FileNotFoundException e) {
			System.err.println("File not found -> scores.in");
			System.exit(-1);
		}
		for (i = 0; i < myTable.getPos(); ++i) {
			this.out.println(myTable.getName(i) + "<break>" +  myTable.getScore(i));
		}
		
		out.flush();
		out.close();
		game.view.setVisible(false);
    	new ShowScore(myTable);
    }
    
    private void parse(BufferedReader in) throws IOException {
    	int i = 0;
    	
    	scores = new String[MAXLINES];
    	for(i = 0; i < MAXLINES; ++i) {
    		scores[i] = null;
    	}
    	
    	for (i = 0; (i < (MAXLINES - 2)) && ( null != (scores[i] = in.readLine()) ) ; ++i) {
    		p = Pattern.compile("([\\w \\s]{1,8}+)<break>\\s*(\\d{1,5}+)");
    		m = p.matcher(scores[i]);
    		if ( !(b = m.matches()) ){
    			return ;
    		} else {
    			myTable.add( new Element(new Integer(m.group(2)).intValue(), 
    					m.group(1)) );
    		}	
    	}
    	in.close();
    }

    
    public void writeOut() {
    	
    }
    
}

